<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\ChangeStatus;
use App\Admin\Repositories\Title;
use App\Models\Title as ModelsTitle;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class TitleController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Title(), function (Grid $grid) {
            $grid->model()->orderBy('sort', 'asc');

            $grid->column('id');
            $grid->column('name');
            $grid->column('sign');
            $grid->column('sort')->sortable()->label('warning')->editable(true);
            $grid->column('status')->using(
                [
                    0 => '未启用',
                    1 => '启用'
                ]
            )->badge(
                [
                    0 => 'gray',
                    1 => 'success'
                ]
            );
            $grid->column('updated_at')->sortable();

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->prepend(new ChangeStatus(ModelsTitle::class), '停用/启用');
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('status');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Title(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('sign');
            $show->field('sort')->label('warning');
            $show->field('status')->using(
                [
                    0 => '未启用',
                    1 => '启用'
                ]
            );
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Title(), function (Form $form) {
            $form->display('id');
            $form->text('name')->required();
            $form->text('sign');
            $form->number('sort')->default(1);
            $form->switch('status')->default(1);

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
